import axios from 'axios'
//import router from '../router'
axios.defaults.timeout=5000;
axios.defaults.baseURL='/api'
import { Message } from 'element-ui';


//import { Message } from 'element-ui';

//http request拦截器 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    let token = window.sessionStorage.getItem("tokenValue")
    if (token) {
        //将token放到请求头发送给服务器,将tokenkey放在请求头中
        config.headers.satoken = token;
        //也可以这种写法
        // config.headers['accessToken'] = Token;
        return config;
    }
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});


//拦截器
axios.interceptors.response.use(success=>{
    console.log(success.data.data)
  if (success.data.statusCode==500){
      Message.error("请重新登录");
      sessionStorage.clear();
      this.$router.replace('/login');
  }

  if (success.data.statusCode!=200 && success.data.statusCode!=500){
      Message.error(success.data.message);
  }

    return success;

},error => {
    if (error.response.status==504||error.response.status==404){
     Message.error({message:'服务器被吃了o(╯□╰)o'});
    }else if (error.response.status==403){
      Message.error({message:'权限不足,请联系管理员!'})
    }else if (error.response.status==401){
      Message.error({message:'尚未登录,请登录!'});
      this.$router.replace('/login');
    } else {
      if (error.response.data.message){
        Message.error({message:error.response.data.message});
      } else {
        Message.error({message:'未知错误!'})
      }
    }
    return;
});
//
// //后期维护加请求路径
// let base ='';
//
// //传送json格式的post请求
// export const postRequest=(url,params)=>{
//   return axios({
//     method:'post',
//     url:'${base}${url}',
//     data:params
//   })
// }
